package cn.tsmchina.one.controller.config;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.tsmchina.one.domain.config.Config;
import cn.tsmchina.one.domain.user.Customer;
import cn.tsmchina.one.service.config.ConfigService;
import cn.tsmchina.one.util.C;
import cn.tsmchina.one.util.JsonUtil;
import cn.tsmchina.one.util.PayParams;
import cn.tsmchina.one.util.Return;

@Controller
@RequestMapping("/config")
public class ConfigController
{
	@Autowired
	private ConfigService configService;
	
	/**
	 * 配置信息查询
	 * @param model
	 * @return
	 */
	@RequestMapping("/query/{configKey}")
	public String indexImageConfig(@PathVariable String configKey, Model model,HttpServletRequest request)
	{
		Customer customer = (Customer) request.getSession().getAttribute("customer");
		long cid = customer.getId();
		
		Object configValue = null;
		String jsp = null;
		
		if(configKey.equals(C.ConfigKeys.FRONT_ACTIVITY)) {
			configValue = C.DefaultConfig.DEFAULT_FRONT_ACTIVITY_IMAGE;
			jsp = "configIndexImage";
		} else if(configKey.equals(C.ConfigKeys.FRONT_RESTAURANT)) {
			configValue = C.DefaultConfig.DEFAULT_FRONT_RESTAURANT_IMAGE;
			jsp = "configRestaurantImage";
		} else if(configKey.equals(C.ConfigKeys.FRONT_MEMBER)) {
			configValue = C.DefaultConfig.DEFAULT_FRONT_MEMBER_IMAGE;
			jsp = "configMemberImage";
		} else if(configKey.equals(C.ConfigKeys.FRONT_TITLE)) {
			configValue = C.DefaultConfig.DEFAULT_FRONT_TITLE;
			jsp = "configIndexTitle";
		} else if(configKey.equals(C.ConfigKeys.SHOW_MENU_PLUGIN)) {
			configValue = C.DefaultConfig.DEFAULT_SHOW_MENU_PLUGIN;
			jsp = "configMenuPlugin";
		} else if(configKey.equals(C.ConfigKeys.YJBH)) {
			configValue = C.DefaultConfig.DEFAULT_YJBH;
			jsp = "configYjbh";
		} else if(configKey.equals(C.ConfigKeys.YJDH)) {
			configValue = C.DefaultConfig.DEFAULT_YJDH;
			jsp = "configYjdh";
		} else if(configKey.equals(C.ConfigKeys.PAY)) {
			jsp = "configPay";
		}else if(configKey.equals(C.ConfigKeys.WC_PAY)) {
			jsp = "configWcPay";
		} else {
			return "m/404";
		}
		
		Config config = configService.getConfig(cid, configKey);
		
		if(configKey.equals(C.ConfigKeys.PAY)) {
			return configPay(config, model,C.ConfigKeys.PAY);
		}else if(configKey.equals(C.ConfigKeys.WC_PAY)) {
			return configPay(config, model,C.ConfigKeys.WC_PAY);
		}
		
		if(config==null) {
			model.addAttribute(configKey, configValue);
			model.addAttribute("configId", "-1");//-1提示数据库中还没有该custom对应的首页图片配置项
			return "config/"+jsp;
		}
		configValue = config.getConfigValue();
		if(configValue==null){
			model.addAttribute(configKey, "");
			model.addAttribute("configId", config.getId());
		}else{
			model.addAttribute(configKey, configValue);
			model.addAttribute("configId", config.getId());
		}
		
		
		return "config/"+jsp;
	}
	
	/**
	 * 编辑配置信息
	 */
	@RequestMapping(value="/edit/{configKey}",method = RequestMethod.POST)
	public String editIndexImageConfig(@PathVariable String configKey, @ModelAttribute Config config, HttpServletRequest request)
	{
		try
		{
			String configValue = config.getConfigValue();
			if(configKey.equals(C.ConfigKeys.SHOW_MENU_PLUGIN)) {
				if(configValue==null)
					configValue = "false";
				else
					configValue = "true";
			}
			if(config.getId()==-1) {
				Customer customer = (Customer) request.getSession().getAttribute("customer");
				long cid = customer.getId();
				configService.addConfig(cid, configValue, configKey);
			} else {
				configService.updateConfig(config.getId(), configValue);
			}
			request.setAttribute("msgType", C.Msg.OK);
			request.setAttribute("msg", "保存成功!");
			request.setAttribute("return", request.getHeader("referer"));
		}catch (Exception e)
		{
			request.setAttribute("msgType", C.Msg.ERROR);
			request.setAttribute("msg", "保存失败!");
			request.setAttribute("return", request.getHeader("referer"));
		}
		return Return.redirect(request);
	}
	
	@RequestMapping(value="/edit_/pay",method = RequestMethod.POST)
	public String editPayConfig(@ModelAttribute Config config, String payType,
			String seller, String partner, String key, String rsa, HttpServletRequest request)
	{
		try
		{
			PayParams params = new PayParams();
			params.setSeller(seller);
			params.setPartner(partner);
			params.setKey(key);
			params.setRsa(rsa);
			String configValue = JsonUtil.parseJson(params);
			if(config.getId()==-1) {
				Customer customer = (Customer) request.getSession().getAttribute("customer");
				long cid = customer.getId();
				
				//微信支付
				if(!StringUtils.isEmpty(payType)&& C.ConfigKeys.WC_PAY.equals(payType)){
					configService.addConfig(cid, configValue, C.ConfigKeys.WC_PAY);
				}else{
					configService.addConfig(cid, configValue, C.ConfigKeys.PAY);
				}
			} else {
				configService.updateConfig(config.getId(), configValue);
			}
			request.setAttribute("msgType", C.Msg.OK);
			request.setAttribute("msg", "保存成功!");
			request.setAttribute("return", request.getHeader("referer"));
		}catch (Exception e)
		{
			request.setAttribute("msgType", C.Msg.ERROR);
			request.setAttribute("msg", "保存失败!");
			request.setAttribute("return", request.getHeader("referer"));
		}
		return Return.redirect(request);
	}
	
	private String configPay(Config config, Model model,String configKey) {
		if(config==null) {
			model.addAttribute(C.ConfigKeys.PAY, "");
			model.addAttribute("configId", "-1");//-1提示数据库中还没有该custom对应的首页图片配置项
		} else {
			String configValue = config.getConfigValue();
			PayParams params = JsonUtil.parseObject(PayParams.class, configValue);
			model.addAttribute("seller", params.getSeller());
			model.addAttribute("partner", params.getPartner());
			model.addAttribute("key", params.getKey());
			model.addAttribute("rsa", params.getRsa());
			model.addAttribute("configId", config.getId());
		}
		
		if(!StringUtils.isEmpty(configKey) && C.ConfigKeys.WC_PAY.equals(configKey)){
			return "config/configWcPay";
		}
		
		return "config/configPay";
	}
}
